<template>
  <el-dialog class="assign-role" title="分配角色" :visible="showRoleDialog" @opened="opened" @close="close">
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in rows" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>

    </el-checkbox-group>

    <template #footer>
      <el-button type="primary" size="small" @click="submitAssignRole">确定</el-button>
      <el-button size="small" @click="close">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { reqAllRoleList } from '@/api/setting'
import { reqGetUserDetailById } from '@/api/user'
import { reqAssignRoles } from '@/api/employees'
export default {
  name: 'AssignRole',
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      checkList: [],
      rows: []
    }
  },
  methods: {
    close() {
      this.$emit('update:showRoleDialog', false)
      this.checkList = []
    },
    async opened() {
      const { rows } = await reqAllRoleList(1, 999)
      this.rows = rows
      const { roleIds } = await reqGetUserDetailById(this.userId)
      this.checkList = roleIds
    },
    async submitAssignRole() {
      await reqAssignRoles({
        id: this.userId,
        roleIds: this.checkList
      })
      this.$message.success('分配角色成功')
      this.close()
    }
  }
}
</script>
<style lang="scss" scoped>
.assign-role {
  ::v-deep {
    .el-checkbox {
      font-size: 30px;
    }
  }
}
</style>
